
(function() {
    'use strict';

    var Utils = function() {
        
    };
    Utils.prototype = {
        searchCompanyName:function (options) {
            var self=this;
            var listPanel=$(options.listPanel);
            var companyNameInput=$(options.companyNameInput);
            var searchIdInput=$(options.searchIdInput);
            var companyListPort=options.companyListPort;
            var upIdKey=options.upIdKey;
            var sonInput=$(options.sonInput);
            var sonIdKey=options.sonIdKey;
            var timer="";
            var searchJSON={};
            $("body").on("tap",function () {
                listPanel.hide();
            });
            companyNameInput.off("input").on("input",function (e) {
                var $this=$(this);
                    searchIdInput.val("");
                if(upIdKey){
                    searchJSON[upIdKey]=$("[name='"+upIdKey+"']").val();
                }
                if(!!sonInput.length){
                    sonInput.val("");
                }
                if(!!sonIdKey){
                    $("input[name='"+sonIdKey+"']").val("");
                }
                var selfName=$this.attr("name");
                    clearTimeout(timer);
                if($this.val()===""){
                    listPanel.html("");
                }else {
                    // console.log(searchIdInput.val());
                    timer=setTimeout(function () {
                        searchJSON[companyNameInput.attr("name")]=companyNameInput.val();
                            //根据搜索公司的输入框的键盘输入事件，每次输入延迟500毫秒向服务器发送请求获取匹配的公司列表数据。
                            // 如果输入状态是连续输入信息，那就每次输入都清空上一次的定时器，禁止频繁输入请求
                            var searchWord=companyNameInput.val();
                            if(upIdKey && !$("[name='"+upIdKey+"']").val()){
                                return false;
                            }
                            self.getCompanyNameList({
                                companyNameInput:companyNameInput,
                                searchIdInput:searchIdInput,
                                searchJSON:searchJSON,
                                listPanel:listPanel,
                                searchWord:searchWord,
                                companyListPort:companyListPort,
                                selfName:selfName,
                                callback:function (data) {
                                    // 组装根据列表数据拼装模板
                                    var data=data.res.data.record;
                                    if(!!data.length){
                                        var list= data.map(function (item, idx) {
                                            return '<li data-id="'+item["id"]+'">'+item[selfName].replace(searchWord, '<span>'+ searchWord + '</span>')+'</li>';
                                        });
                                        
                                        listPanel.show().html(list.join(""));
                                    }else{
                                        listPanel.show().html("");
                                    }
                                }
                            });
                    },500);
                }
            });
        },
        getCompanyNameList:function (options) {
            var globalHost=common.globalHost;
            var companyListPort=options.companyListPort;
            var url=globalHost+companyListPort;
            var searchJSON=options.searchJSON||{};
            var listPanel=$(options.listPanel);
            var companyNameInput=$(options.companyNameInput);
            var searchIdInput=$(options.searchIdInput);
            var selfName=options.selfName;
            var selfId=options.selfId;
            var callback=options.callback;
            var send={status:'AVAILABLE',numPerPage:'5'};
            send=$.extend(send,searchJSON);//使send对象继承传入的 搜索词 searchJSON 对象
            $.post(url,send,function (data) {
                var detailInfo = typeof data==="string"? JSON.parse(data):data;
                !!callback&&$.isFunction(callback)&&callback(detailInfo);
                detailInfo=detailInfo.res.data.record;
                listPanel.off("tap").on("tap","li",function (ev) {
                    listPanel.hide();
                    var $this=$(this);
                    var selfNodeName=$this[0]['nodeName'];
                    var selfInfo=detailInfo[$this.index()];
                    //console.log(selfInfo);
                    var resultStr=selfInfo[selfName];
                    var resultId=selfInfo['id'];
                    //转编译字符
                    companyNameInput.val($("#transHidden").html(resultStr).html());
                    searchIdInput.val(resultId);
                    //阻止事件冒泡
                    ev.stopPropagation();
                });
            });
        }
    };
    window.utils = new Utils();
}).call(this);
